package bank;

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.sql.*;
import java.util.ArrayList;

public class Util {
    public static Image getImage(String path) {

        return new ImageIcon(Util.class.getResource(path)).getImage();
    }

    public static ImageIcon getIcon(String path) {
        return new ImageIcon(Util.class.getResource(path));
    }

    /**
     * 创建数据库连接
     * @return
     */
    public static  Connection getConnection() {
        Connection con=null;
        try {
            // 1. 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2. 创建连接
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/bank?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root", "525277");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }

    /**
     * 查询数据库
     */
    public static ArrayList query() {
        Connection con = getConnection();
        ArrayList<User> userlise=new ArrayList();
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            statement = con.createStatement();
            String sql = "select u_name,u_psw,u_money from t_users";
            resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
               User user =new User();
               user.setAccount(resultSet.getString(1));
               user.setPsw(resultSet.getString(2));
               user.setBalance(resultSet.getDouble(3));
               userlise.add(user);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return userlise;
    }

    /**
     * 修改数据库数据
     */
    public static void insert(String sql) {
        Connection con = getConnection();
        Statement statement = null;
        try {
            statement = con.createStatement();
            int row = statement.executeUpdate(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            if (statement !=null){
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

}
